package SBLib;


import java.util.ArrayList;
import server.FileIO;

/**
 * A File object represents a File made by a User
 * fileName --> name of file
 * fileContent --> content of file
 * userName --> the username of the user that created the file. This is important because only the person that created the 
 *              file can rename and delete it.
 * usersWithLock --> tracks all the users that currently have a lock on the file (have it open). When it is empty, that means no one is viewing it
 *                      and it is safe to edit/delete/rename
 * @author BrandonNeal
 */
public class File {
    private String fileName;
    private String fileContent;
    private String userName;
    
    //private String userWithLock;
    private ArrayList<String> usersWithLock;
    

    public File(String name,String content, String userName)
    {
        this.fileName = name;
        this.fileContent = content;
        this.userName = userName;
        
        usersWithLock = new ArrayList<String>();
        //this.userWithLock = "";
        
    }
    public File(String name, String content)
    {
        this.fileName = name;
        this.fileContent = content;
                
        usersWithLock = new ArrayList<String>();
    }
    public File()
    {
        
    }
    public void SaveFile()
    {
       FileIO.SaveFile(FileIO.FILE_DIR+this.fileName, this.fileContent);
    }
    public void DeleteFile()
    {
        FileIO.DeleteFile(FileIO.FILE_DIR+this.fileName);
    }
    public void RenameFile(String newName)
    {
        FileIO.RenameFile(FileIO.FILE_DIR+this.fileName, FileIO.FILE_DIR+newName);
        this.fileName = newName;
    }
   
    public void EditFile(String newContent)
    {
        this.setFileContent(newContent);
        
        this.DeleteFile();
        this.SaveFile();
    }
   /**
    * Gives user a lock on the file by adding it to the File's list of Users who have locks
    * @param user Name of the user that wants to attain a lock on the file
    * @return true (Lock attained)
    */
    public Boolean Lock(String user)
    {
        
        if(!this.usersWithLock.contains(user))
            this.usersWithLock.add(user);
        return true;
    }
    /**
     * @return if file is locked (at least 1 person has a lock other than the current user)
     */
    public Boolean getLocked(String username) {
        if(this.usersWithLock.isEmpty() ||(this.usersWithLock.contains(username) && this.usersWithLock.size()==1))
            return false;
        else
            return true;
    }
    /**
     * Removes a user from list of User's who have locks on the file.
     * @param username Name of user to remove from list of users who have locks on the file
     */
    public void Unlock(String username)
    {
        this.usersWithLock.remove(username);
    }
    /**
     * Overriden File equals method. Allows for comparison by fileName when seeing if a given filename is in an ArrayList of Files
     * @param other The other File object to compare against
     * @return 
     */
    @Override
    public boolean equals(Object other)
    {
        File user = (File)other;
        if(this.getFileName().equals(user.getFileName()))
        {
            return true;
        }
        return false;
    }
    /**
     * Debugging method that just prints out users that currently have lock on file
     * @return 
     */
    public String LockLogger()
    {
        String m="Current Lock on: "+this.fileName+" \n";
        for(String s:this.usersWithLock)
        {
            m+=s+"\n";
        }
        return m;
    }
    /**
     * @return the fileName
     */
    public String getFileName() {
        return fileName;
    }

    /**
     * @param fileName the fileName to set
     */
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    /**
     * @return the fileContent
     */
    public String getFileContent() {
        return fileContent;
    }

    /**
     * @param fileContent the fileContent to set
     */
    public void setFileContent(String fileContent) {
        this.fileContent = fileContent;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }
    
 
  

    
}
